package com.ustb.springboot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ustb.springboot.common.utils.R;
import com.ustb.springboot.common.utils.TokenUtil;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;


import com.ustb.springboot.dao.QuestionListboxDao;
import com.ustb.springboot.entity.QuestionListboxEntity;
import com.ustb.springboot.service.QuestionListboxService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;


@Service("questionListboxService")
public class QuestionListboxServiceImpl extends ServiceImpl<QuestionListboxDao, QuestionListboxEntity> implements QuestionListboxService {

    @Resource
    private QuestionListboxDao questionListboxDao;

    @Override
    public R save(HttpServletRequest request, QuestionListboxEntity questionListbox) {
        Integer userId = TokenUtil.getId(request);
        questionListbox.setUserId(userId);
        //调用接口保存
        try {
            if (this.save(questionListbox))
                return R.ok(20000, "创建填空题成功！");
        } catch (Exception e) {
            return R.error(2000, "创建失败！");
        }
        return R.error(2000, "创建失败！");
    }

    @Override
    public R delete(Map<String, Object> body) {
        QueryWrapper<QuestionListboxEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", body.get("id"));
        int delete = questionListboxDao.delete(queryWrapper);
        if (delete == 1)
            return R.ok(20000, "删除成功！");
        else
            return R.error(2000, "删除失败！");
    }

    @Override
    public List<QuestionListboxEntity> queryQuestionListbox(Map<String, Object> body) {
        QueryWrapper<QuestionListboxEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", body.get("question_id"));
        return questionListboxDao.selectList(queryWrapper);
    }
}